﻿// -----------------------------
// Copyright © Kodi Studios 2012
// -----------------------------

using System;
using System.Diagnostics;
using System.IO;
using System.Windows;
using System.Windows.Interop;

namespace Kodi.Picturethrill
{
    /// <summary>
    /// Interaction logic for AdvancedWindow.xaml
    /// </summary>
    public partial class AdvancedWindow : Window
    {
        public AdvancedWindow()
        {
            InitializeComponent();

            this.imageDirectoryTextBox.IsEnabled = (bool)saveNewPicturesCheckBox.IsChecked;
            this.browseButton.IsEnabled = (bool)saveNewPicturesCheckBox.IsChecked;
            this.viewFolderButton.IsEnabled = (bool)saveNewPicturesCheckBox.IsChecked;

            this.saveNewPicturesCheckBox.Checked += new RoutedEventHandler(OnSaveNewPicturesCheckBox_Checked);
            this.saveNewPicturesCheckBox.Unchecked += new RoutedEventHandler(OnSaveNewPicturesCheckBox_Unchecked);
        }

        void OnSaveNewPicturesCheckBox_Checked(object sender, RoutedEventArgs e)
        {
            this.imageDirectoryTextBox.IsEnabled = true;
            this.browseButton.IsEnabled = true;
            this.viewFolderButton.IsEnabled = true;
        }

        void OnSaveNewPicturesCheckBox_Unchecked(object sender, RoutedEventArgs e)
        {
            this.imageDirectoryTextBox.IsEnabled = false;
            this.browseButton.IsEnabled = false;
            this.viewFolderButton.IsEnabled = false;
        }

        /// <summary>
        /// 
        /// </summary>
        public bool UpdateDesktopBackground
        {
            get
            {
                return (bool)this.setBackgroundCheckBox.IsChecked;
            }
            set
            {
                this.setBackgroundCheckBox.IsChecked = value;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public bool SaveNewPictures
        {
            get
            {
                return (bool)this.saveNewPicturesCheckBox.IsChecked;
            }
            set
            {
                this.saveNewPicturesCheckBox.IsChecked = value;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public string ImageDirectory
        {
            get
            {
                return this.imageDirectoryTextBox.Text;
            }
            set
            {
                if (string.IsNullOrEmpty(value))
                {
                    this.imageDirectoryTextBox.Text = string.Empty;
                }
                else
                {
                    this.imageDirectoryTextBox.Text = value;
                }
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public bool AutomaticUpdate
        {
            get
            {
                return (bool)this.automaticUpdateCheckBox.IsChecked;
            }
            set
            {
                this.automaticUpdateCheckBox.IsChecked = value;
            }
        }

        #region Event Handlers

        /// <summary>
        /// Open FolderBrowser dialog.
        /// </summary>
        /// <param name="sender">Sender object.</param>
        /// <param name="eventArgs">Event arguments.</param>
        private void OnBrowseButton_Click(object sender, RoutedEventArgs eventArgs)
        {
            using (Log.Group())
            {
                System.Windows.Forms.FolderBrowserDialog folderBrowserDialog = new System.Windows.Forms.FolderBrowserDialog();
                folderBrowserDialog.SelectedPath = this.imageDirectoryTextBox.Text;
                if (folderBrowserDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    this.imageDirectoryTextBox.Text = folderBrowserDialog.SelectedPath;
                }
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="eventArgs"></param>
        private void OnOkButton_Click(object sender, RoutedEventArgs eventArgs)
        {
            using (Log.Group())
            {
                this.Close();
            }
        }

        /// <summary>
        /// Opens Folder which contains downloaded pictures in Windows Explorer.
        /// </summary>
        /// <param name="sender">Sender object.</param>
        /// <param name="eventArgs">Event arguments.</param>
        private void OnViewFolderButton_Click(object sender, RoutedEventArgs eventArgs)
        {
            using (Log.Group())
            {
                if (!Directory.Exists(this.imageDirectoryTextBox.Text))
                {
                    Directory.CreateDirectory(this.imageDirectoryTextBox.Text);
                }

                Process.Start(this.imageDirectoryTextBox.Text);
            }
        }

        private void OnAboutPicturethrillButton_Click(object sender, RoutedEventArgs e)
        {
            using (Log.Group())
            {
                AboutPicturethrillWindow aboutPicturethrillWindow = new AboutPicturethrillWindow();
                aboutPicturethrillWindow.ShowDialog();
            }
        }
        #endregion
    }
}
